package ink.yql.controller;

import ink.yql.model.Accountnumber;
import ink.yql.service.AccountnumberService;
import ink.yql.util.RestObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author YQL
 */
@RestController
@RequestMapping("/accountnumber")
public class AccountnumberController {
    @Autowired
    private AccountnumberService accountnumberService;

    @GetMapping("/{sendTime}")
    public RestObject getByUid(@PathVariable("sendTime") Long sendTime) {
        // 保护措施  以防拿到url一直请求
        if (System.currentTimeMillis() / 1000 - sendTime > 3) {
            return RestObject.builder().code("400").message("请求时间超时").build();
        }
        return accountnumberService.getByUid();
    }

    @GetMapping("/default/{sendTime}")
    public RestObject getByUidDefault(@PathVariable("sendTime") Long sendTime) {
        // 保护措施  以防拿到url一直请求
        if (System.currentTimeMillis() / 1000 - sendTime > 3) {
            return RestObject.builder().code("400").message("请求时间超时").build();
        }
        return accountnumberService.getByUidDefault();
    }

    @PostMapping("/{sendTime}")
    public RestObject addOne(@RequestBody Accountnumber accountnumber, @PathVariable("sendTime") Long sendTime) {
        // 保护措施  以防拿到url一直请求
        if (System.currentTimeMillis() / 1000 - sendTime > 3) {
            return RestObject.builder().code("400").message("请求时间超时").build();
        }
        return accountnumberService.addOne(accountnumber);
    }

    @PutMapping("/{sendTime}")
    public RestObject update(@RequestBody Accountnumber accountnumber, @PathVariable("sendTime") Long sendTime) {
        // 保护措施  以防拿到url一直请求
        if (System.currentTimeMillis() / 1000 - sendTime > 3) {
            return RestObject.builder().code("400").message("请求时间超时").build();
        }
        return accountnumberService.update(accountnumber);
    }

    @DeleteMapping("/{anid}/{sendTime}")
    public RestObject delete(@PathVariable("anid") Integer anid, @PathVariable("sendTime") Long sendTime) {
        // 保护措施  以防拿到url一直请求
        if (System.currentTimeMillis() / 1000 - sendTime > 3) {
            return RestObject.builder().code("400").message("请求时间超时").build();
        }
        return accountnumberService.deleteByAnid(anid);
    }


}
